package edu.its.dreamteam.web.pages.admin;

import java.util.List;

import org.apache.tapestry.annotations.Component;
import org.apache.tapestry.corelib.components.Form;
import org.apache.tapestry.ioc.annotations.Inject;

import edu.its.dreamteam.daf.beans.Manager;
import edu.its.dreamteam.daf.dao.IManagerDao;

public class UserForm {
	@Inject
	private IManagerDao managerDao;
	private List<Manager> managerList;
	private Manager manager;
	
	private String username;
	
	@Component
	private Form managerForm;
	
	public Manager getManager() {
		return manager;
	}

	public void setManager(Manager manager) {
		this.manager = manager;
	}

	public IManagerDao getManagerDao() {
		return managerDao;
	}

	void onPrepare(){
        if (manager == null){
        	manager = new Manager();         	
        }        
    }
	
	void cleanupRender() {
		setManager(null);
	}
	
	void onActionFromClear(){
		setManager(null);
		managerForm.clearErrors();
    }	

	Object onSuccessFromClubForm(){
		if(getManager().getUsername()==null){	
			getManagerDao().insert(getManager());			
		}else{			
			getManagerDao().update(getManager());
						
		}
		setManager(null);
		return ClubGrid.class;
	}
	
	void onActivate(String username){	
		this.username = username;
		setManager((Manager)getManagerDao().findByPrimaryKey(username));		
	}
	
	String onPassivate(){ 
		return username; 
	}	
}
